package com.mayikt.weixin.service.impl;

import com.mayikt.generator.OrderNoGenerator;
import com.mayikt.utils.RedisLockUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @Classname OrderController
 * @Description SpringBoot SpringCloud 构建微服务电商平台。
 * @Created by CHEN JIN LONG
 * @Date 2019/1/22 20:03
 * @Version 1.0
 */

public class OrderController {

    @Autowired
    private RedisLockUtils redisLockUtils;
    @Autowired
    private OrderNoGenerator orderNoUtils;

    /**
     * 基于redis生成全局ID
     * 前缀20190122+随机数+5位自增id
     * 00001
     * 00002
     * 00003
     * 00004
     * 00005
     * ...
     * 在相同毫秒情况下，最多只能生成10万减1个订单号
     * 假设：双十一每毫秒99万个最大订单，扩充到8位
     * 解决方案，可以提前生成订单号码，存放在redis中，别人直接取。
     *
     * @return
     */
    @RequestMapping("/order")
    public String order(String key) {


        return orderNoUtils.generateOrderNo(key);
    }
    @RequestMapping("/redisLock")
    public String redisLock() {

        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();

        for (int i = 0; i < 200; i++) {
            ThreadRedis task = new ThreadRedis(redisLockUtils);
            //System.out.println("A new task has been added : " + task.getName());
            executor.execute(task);
        }
        executor.shutdown();
        return "成功";
    }
}
